<?php

namespace app\admin\controller;

use app\models\Admin;
use app\models\{User as Users, Money, Moneylog, Price, Config, Order, Orderlog, Quotes, Commvote, Incomelist, Withdraw, Locklist, Relesstaitc};
use think\Db;
use think\Facade\Config as Configs;

class Main extends Base
{

    public function welcome()
    {
        $data = [];
        $data['allsfthree'] = (new Moneylog())->where('type', 4)->sum('money'); //静态累计释放
        $data['allsffive'] = (new Moneylog())->where('type', 5)->sum('money'); //动态累计释放
        $data['orderfee'] = (new Orderlog())->where(['coins' => 1])->sum('coinfee'); //订单产生手续费
        $withdraw = (new Withdraw())->where(['cointype' => [1, 2], 'status' => 3])->select(); //动态累计释放
        $data['widhandfee'] = 0; //提现手续费累计
        foreach ($withdraw as $withdraws) {
            if ($withdraws['cointype'] == 2) {
                $data['widhandfee'] += $withdraws['handfee'] * $withdraws['price'];
            } else {
                $data['widhandfee'] += $withdraws['handfee'];
            }
        }
        //社区长参数
        $Commdirpush = (new Config())->where('key_value', 'Commdirpush')->value('value'); //直推有效会员
        $Commdirpush = $Commdirpush ? $Commdirpush : 0;
        $CommLockMoney = (new Config())->where('key_value', 'CommLockMoney')->value('value'); //锁仓JTE价值
        $CommLockMoney = $CommLockMoney ? $CommLockMoney : 0;
        //合伙人参数
        $Pardirpush = (new Config())->where('key_value', 'Pardirpush')->value('value'); //直推有效会员
        $Pardirpush = $Pardirpush ? $Pardirpush : 0;
        $Pardirpeople = (new Config())->where('key_value', 'Pardirpeople')->value('value'); //直推总人数
        $Pardirpeople = $Pardirpeople ? $Pardirpeople : 0;
        $ParLockMoney = (new Config())->where('key_value', 'ParLockMoney')->value('value'); //锁仓JTE价值
        $ParLockMoney = $ParLockMoney ? $ParLockMoney : 0;
        $userlist = (new Users())->where('isagent|ispartner', 2)->select();
        $data['partpeopele'] = 0;
        $data['agentpeople'] = 0;
        foreach ($userlist as $userlists) {
            if ($userlists['ispartner'] == 2 && $userlists['effepeople'] >= $Pardirpush && $userlists['teampople'] >= $Pardirpeople && $userlists['lockmoney'] >= $ParLockMoney) {
                $data['partpeopele'] += 1;
            }
            if ($userlists['isagent'] == 2 && $userlists['effepeople'] >= $Commdirpush && $userlists['lockmoney'] >= $CommLockMoney) {
                $data['agentpeople'] += 1;
            }
        }
        $data['locknum'] = (new Locklist())->where(['status' => 1])->sum('number'); //累计进行中的锁仓数量
        $data['lockmoney'] = (new Locklist())->sum('money'); //累计进行中的锁仓数量
        $data['isagent'] = (new Users())->where('isagent', 2)->count(); //合伙人数量
        $data['ispartner'] = (new Users())->where('ispartner', 2)->count(); //社区长数量
        $data['equity'] = (new Users())->sum('equity'); //社区长数量
        $data['todayuser'] = (new Users())->where('created', '>=', date('Y-m-d'))->count(); //新增会员
        $totals = [];
        $ModelFun = new Money();
        $coin = Configs::get('config.coins');
        foreach ($coin as $k => $coins) {
            $totals[] = [
                'name' => $coins,
                'total' => $ModelFun->where('type', $k)->sum('money'),
                'freezetotal' =>  $ModelFun->where('type', $k)->sum('freezemoney')
            ];
        }
        $postdata = $this->request->get();
        if (isset($postdata['export']) && $postdata['export'] == 2) {
            $name = date('Y-m-d', time() - 86400) . '日报表';
            $header =   ["日期", "会员总量", "当日新增会员",  "锁仓总量", "JTE持有量", "释放总量", "锁仓百分比", "JSR总量", "社区长总量", "可投票社区长总量", "USDT持有量"];
            $Relesstaitc = (new Relesstaitc())->where('dates', date('Y-m-d', time() - 86400))->find();
            if (!empty($Relesstaitc) && $Relesstaitc['reletotal'] > 0) {
                $rate = round($Relesstaitc['locktotal'] / $Relesstaitc['reletotal'], 6);
            } else {
                $rate = 0;
            }
            $datb[] = [
                date('Y-m-d', time() - 86400),
                (new Users())->where('created', '<', date('Y-m-d'))->count(),
                (new Users())->where('created', '<', date('Y-m-d'))->where('created', '>=', date('Y-m-d', time() - 86400))->count(),
                empty($Relesstaitc['locktotal']) ? 0 : $Relesstaitc['locktotal'],
                empty($Relesstaitc['jte']) ? 0 : $Relesstaitc['jte'],
                empty($Relesstaitc['reletotal']) ? 0 : $Relesstaitc['reletotal'],
                $rate,
                empty($Relesstaitc['jsr']) ? 0 : $Relesstaitc['jsr'],
                empty($Relesstaitc['agent']) ? 0 : $Relesstaitc['agent'],
                empty($Relesstaitc['agentcan']) ? 0 : $Relesstaitc['agentcan'],
                empty($Relesstaitc['usdt']) ? 0 : $Relesstaitc['usdt'],
            ];
            excelExport($name, $header, $datb);
        }
        return view(null, [
            'admin' => $this->request->admin,
            'data' => $data,
            'totals' => $totals,

        ]);
    }

    public function index()
    {
        $admin = $this->request->admin;
        $menu = config('menu.menu');
        $arr = [];
        foreach ($menu as $k => $v) {
            if ($v['show'] == true) {
                foreach ($v['sub'] as $x => $y) {
                    if (($y['show'] == true && in_array($y['uri'], $admin['role'])) || $admin['is_default'] == 1) {
                        $arr[$k]['name'] = $v['name'];
                        $arr[$k]['ico'] = $v['ico'];
                        $arr[$k]['sub'][$x] = $y;
                    }
                }
            }
        }
        return view(null, [
            'admin' => $admin,
            'menu' => $arr,
        ]);
    }


    public function miss()
    {
        return view('404');
    }

    public function admin()
    {
        $admin = Admin::get(session('admin_id'));
        if ($this->request->isPost()) {
            if (empty($admin)) {
                json(['status' => 1, 'msg' => '登陆已过期!']);
            }

            $data = input('post.');
            if ($data['password'] != '') {
                if ($data['password'] != $data['passworder']) {
                    return json(['status' => 1, 'info' => '密码不一致']);
                }
                $data1['password'] = md5(md5($data['password']) . md5($admin['entry']));
            }
            $data1['nickname'] = $data['nickname'];
            if ($admin->save($data1, ['id' => $admin->id])) {
                return json(['status' => 0, 'msg' => '操作成功']);
            } else {
                return json(['status' => 1, 'msg' => '操作失败']);
            }
        } else {
            return view('admin', ['admin' => $admin]);
        }
    }
}
